# @Time:   2021/2/18 13:30
# @Author: Zong Wei

import time
from datetime import date, timedelta
from time import sleep

from faker import Faker
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class ClientAgreementPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='客户协议管理']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")
    search_self_button = (By.ID, "lr-searchSelf")
    copy_button = (By.ID, "lr-copy")

    agreement_name = (By.ID, "AgreementTitle")
    agreement_number = (By.ID, "AgreementNo")
    start_date = (By.ID, "StartDate")
    end_date = (By.ID, "EndDate")
    supplier_name = (By.ID, "_easyui_textbox_input3")
    supplier_value = (By.ID, "datagrid-row-r3-2-0")
    entity_name = (By.ID, "_easyui_textbox_input2")
    entity_value = (By.ID, "datagrid-row-r2-2-0")
    sign_property = (By.ID, "SignProperty")
    sign_property_value = (By.XPATH, "//div[@id='SignProperty-option']//li[2]")
    version = (By.ID, "_easyui_textbox_input1")
    agreement_property = (By.ID, "AgreementProperty")
    agreement_property_value = (By.XPATH, "//div[@id='AgreementProperty-option']//li[2]")
    partner_name = (By.ID, "PartnerNameCN")

    status = (By.ID, "AgreementStatus")
    status_value = (By.XPATH, "//div[@id='AgreementStatus-option']//li[2]")

    remark = (By.ID, "Remark")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='法人单位']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def client_agreement_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.driver.execute_script('$("#admui-navTabs").scrollTop(200)')
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_agreement(self, agreement_number='TS12345', supplier='朗盛化学（中国）有限公司', entity='上海密尔克卫化工储存有限公司'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加客户协议")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.agreement_name)).send_keys('TEST AGREEMENT')
        self.wait.until(ec.presence_of_element_located(self.agreement_number)).send_keys(agreement_number)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.start_date)).send_keys(str(date.today()))
        self.wait.until(ec.presence_of_element_located(self.end_date)).send_keys(str(date.today()+timedelta(days=90)))
        self.wait.until(ec.presence_of_element_located(self.end_date)).click()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.supplier_name)).send_keys(supplier)
        sleep(1)
        ActionChains(self.driver).send_keys(Keys.TAB).perform()

        # self.wait.until(ec.element_to_be_clickable(self.supplier_value)).click()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.entity_name)).send_keys(entity)
        sleep(1)
        ActionChains(self.driver).send_keys(Keys.TAB).perform()

        # self.wait.until(ec.element_to_be_clickable(self.entity_value)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sign_property)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sign_property_value)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.version)).send_keys('公司版本')

        # self.wait.until(ec.element_to_be_clickable(self.version)).click()
        sleep(1)
        # self.wait.until(ec.element_to_be_clickable(self.version_value)).click()
        self.wait.until(ec.element_to_be_clickable(self.agreement_property)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.agreement_property_value)).click()

        self.wait.until(ec.element_to_be_clickable(self.status)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.status_value)).click()

        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("新增完成")

    def copy_agreement(self, agreement_number='MW-YS-C-2021118'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.agreement_number)).send_keys(agreement_number)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.copy_button)).click()
        self.logger.info("复制客户协议")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.agreement_number)).send_keys('0')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("复制完成")

    def search_agreement(self, agreement_number='MW-YS-C-20211180', partner_name='江门亚什兰化工有限公司'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.agreement_number)).send_keys(agreement_number)
        self.wait.until(ec.presence_of_element_located(self.partner_name)).send_keys(partner_name)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(1)

        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def edit_agreement(self):
        self.search_agreement()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.agreement_name)).send_keys('_Ed')
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.remark)).send_keys(Faker().address())
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("编辑修改完成")

    def del_agreement(self):
        self.search_agreement()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    pg = ClientAgreementPage()
    pg.client_agreement_home()
    pg.del_agreement()

